﻿Imports CoreClassLibraryCMS

Public Class TransactSales
    Inherits JSonDataCrud

    Public Overrides Function DeleteData(param As System.Collections.Specialized.NameValueCollection) As System.Web.Mvc.JsonResult

    End Function

    Public Overrides Function ReadData(Skip As Integer, Take As Integer, SortDir As String, Sort As String, param As String) As System.Web.Mvc.JsonResult
        Dim x As New Transaction.clsTSALES
        Dim m As ADODBU.RowClassList(Of Transaction.clsTSALESRow)
        Dim y As New Web.Mvc.JsonResult

        m = x.GetRowList(Connection.GetDBConn, param)

        Dim res = New With {.success = True, .totalrecords = m.Count, .ListDataObj = m.Skip(Skip).Take(Take)}
        y.Data = res
        Return y
    End Function

    Public Overrides Function SaveData(param As System.Collections.Specialized.NameValueCollection) As System.Web.Mvc.JsonResult
        Dim arr As New ArrayList
        Dim mLocalConn As New ADODBU.dbConn
        Dim xRow As New Transaction.clsTSALESRow()
        Dim y As New Web.Mvc.JsonResult
        Dim strsql As String


        mLocalConn = Connection.GetDBConnData.Clone
        mLocalConn.Open()
        mLocalConn.CreateTransaction()


        arr = GetListDetail1(param, xRow)
        If Not arr Is Nothing Then
            If arr.Count > 0 Then
                For Each dtlRow As Transaction.clsTSALESRow In arr
                    Dim x As New Transaction.clsTSALES
                    strsql = "WHERE SLSNO = " & param.Item("SALESNO").ToString
                    strsql += " AND SLSNOTA = '" & param.Item("NOTA").ToString & "'"
                    strsql += " AND SLSDATE ='" & param.Item("SALESDATE") & "'"
                    strsql += " AND SLSBARCODE = " & dtlRow.SLSBARCODE
                    x.DbConn = Connection.GetDBConn
                    x.Open(strsql)
                    If x.ErrMessage.Length = 0 Then
                        If x.RecordCount = 0 Then
                            xRow.SLSID = GETID()
                            xRow.SLSNO = param.Item("SALESNO").ToString
                            xRow.SLSNOTA = param.Item("NOTA").ToString
                            xRow.SLSDATE = param.Item("SALESDATE")
                            xRow.SLSFLAG = param.Item("FLAG").ToString
                            xRow.SLSDISCAMOUNT = param.Item("TOTDISAMT").ToString
                            xRow.SLSBARCODE = dtlRow.SLSBARCODE
                            xRow.SLSSIZE = dtlRow.SLSSIZE
                            xRow.SLSCOLOR = dtlRow.SLSCOLOR
                            xRow.SLSPRICE = dtlRow.SLSPRICE
                            xRow.SLSDISC = "1000"
                            xRow.SLSTOTAMT = dtlRow.SLSTOTAMT
                            xRow.SLSARTCEXT = dtlRow.SLSARTCEXT
                            xRow.SLSQTY = dtlRow.SLSQTY
                            xRow.SLSSTORE = "SC001"
                            x.SaveClassRow(xRow, True, False)
                            If x.ErrMessage.Length = 0 Then
                                Dim res = New With {.success = True, .SLSID = xRow.SLSID}
                                y.Data = res
                            Else
                                Dim res = New With {.success = False, .pesan = x.ErrMessage.ToString}
                                'mLocalConn.Transaction.Rollback()
                                y.Data = res
                            End If
                        End If
                    End If
                Next
            End If
        End If
        Return y
    End Function

    Public Overrides Function UpdateData(param As System.Collections.Specialized.NameValueCollection) As System.Web.Mvc.JsonResult
        Dim arr As New ArrayList
        Dim mLocalConn As New ADODBU.dbConn
        Dim xRow As New Transaction.clsTSALESRow()
        Dim y As New Web.Mvc.JsonResult
        Dim strsql As String

        mLocalConn = Connection.GetDBConnData.Clone
        mLocalConn.Open()
        mLocalConn.CreateTransaction()

        arr = GetListDetail1(param, xRow)
        If Not arr Is Nothing Then
            If arr.Count > 0 Then
                For Each dtlRow As Transaction.clsTSALESRow In arr
                    Dim x As New Transaction.clsTSALES
                    strsql = "WHERE SLSNO = " & dtlRow.SLSNO
                    strsql += " AND SLSNOTA = " & dtlRow.SLSNOTA
                    strsql += " AND SLSDATE ='" & dtlRow.SLSDATE & "'"
                    strsql += " AND SLSBARCODE = " & dtlRow.SLSBARCODE
                    strsql += " AND SLSFLAG = " & param.Item("FLAG").ToString
                    x.DbConn = mLocalConn
                    x.Open(strsql)
                    If x.ErrMessage.Length = 0 Then
                        If x.RecordCount >= 1 Then
                            xRow.SLSFLAG = 1
                            x.SaveClassRow(xRow, False, True)
                            If x.ErrMessage.Length = 0 Then
                                Dim res = New With {.success = True, .SLSID = xRow.SLSID}
                                y.Data = res
                            Else
                                Dim res = New With {.success = False, .pesan = x.ErrMessage.ToString}
                                mLocalConn.Transaction.Rollback()
                                y.Data = res
                            End If
                        End If
                    End If
                Next
            End If
        End If
        Return y
    End Function

    Private Function GetListDetail1(ByVal param As System.Collections.Specialized.NameValueCollection, ByVal Tr As Transaction.clsTSALESRow) As ArrayList
        Dim arrList As New ArrayList
        Dim arrListRow As New ArrayList
        Dim i As Integer
        Dim jml As Integer = CInt(param.Item("FieldCount"))

        arrList = clsSplitCriteria.splitListDetail(param.Item("ListBarang"), CInt(param.Item("ListCount")), jml)

        If Not arrList Is Nothing Then
            If arrList.Count > 0 Then
                For Each str As String() In arrList
                    If Not str Is Nothing Then

                        Dim TrDet As New Transaction.clsTSALESRow
                        Dim arrListField As New ArrayList

                        For i = 0 To jml - 1
                            Dim splitField As String()
                            splitField = Split(str(i), "=", 2)
                            arrListField.Add(splitField(1))
                        Next
                        TrDet.SLSNO = param.Item("SALESNO").ToString
                        TrDet.SLSNOTA = param.Item("NOTA").ToString
                        TrDet.SLSDATE = param.Item("SALESDATE")
                        TrDet.SLSBARCODE = arrListField.Item(0)
                        TrDet.SLSSIZE = arrListField.Item(1)
                        TrDet.SLSCOLOR = arrListField.Item(2)
                        TrDet.SLSPRICE = arrListField.Item(3)
                        TrDet.SLSDISC = arrListField.Item(4)
                        TrDet.SLSTOTAMT = arrListField.Item(6)
                        TrDet.SLSARTCEXT = arrListField.Item(7)
                        TrDet.SLSQTY = arrListField.Item(8)
                        arrListRow.Add(TrDet)
                    End If
                Next
            End If
        End If

        Return arrListRow
    End Function
    Private Function GETID() As Integer
        Dim strSql As String
        Dim Rec As New ADODBU.Recordset

        strSql = " select top 1 SLSID from TSALES"
        strSql += " order by SLSID desc"
        Rec.Open(strSql, "BUKU", Connection.GetDBConnData)
        If Not Rec.Eof Then
            'If Rec.ErrMessage.Length = 0 Then
            If Rec.RecordCount = 0 Then
                GETID = "0"
                Return GETID
            Else
                GETID = Rec.Fields("SLSID") + 1
                Return GETID
            End If
        End If
    End Function
End Class
